<template>
  <div>
    <el-dialog title="生成入学码" :visible.sync="thistype" @close="fun(tname)">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班级" >
          <el-input v-model="form.classId" :disabled="true"/>
        </el-form-item>
        <el-form-item label="入学码" prop="admissionCode">
          <el-input v-model="form.admissionCode" :disabled="true" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary"  @click="submitForm">点击生成并复制入学码</el-button>
        <el-button @click="cancel">取 消</el-button>
        <input style="opacity: 0;" ref='ipt' />

      </div>
    </el-dialog>
  </div>
</template>
<script>
import {  addCode, updateCode } from "@/api/system/code";
export default {
  props: ['type', 'fun', 'tname', 'tablerow','classid'],
  data () {
    return {
      thistype: this.type, // 模态框状态
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classId: null,
        admissionCode: null,
        isUse: null,
        createDate: null,
        useDate: null,
        isDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classId: [
          { required: true, message: "班级id不能为空", trigger: "blur" }
        ]
      }
    }
  },
  mounted(){

  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.form.classId = this.classid

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCode(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;        
            });
          } else {
                 addCode(this.form).then(response => {
                    this.form.admissionCode = response.msg;
                    this.$set(this.form,this.form.admissionCode);   
                    let oInput = this.$refs.ipt;
                    oInput.value = this.form.admissionCode;
                    oInput.select();
                    document.execCommand("Copy");
                    this.msgSuccess("复制成功");
                    this.open = false;
                });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.thistype = false;
      this.reset(); 
    },
    // 表单重置
    reset() {
      this.value= '',
      this.form = {
        id: null,
        classId: null,
        admissionCode: null,
        isUse: null,
        createBy: null,
        createDate: null,
        useDate: null,
        isDelete: null
      };
      this.resetForm("form");
    },
  },
  watch: {
    type () {
      this.thistype = this.type
    },
    tablerow () {
      this.form = this.tablerow
    }
  }
}
</script>

<style>
</style>
